﻿using HW_CommunicationLib;
using HW_CommunicationLib.Model;
using HW_PlcLib.Enums;
using HW_PlcLib.Interface;
using HW_PlcLib.Model;
using HW_PlcLib.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HW_PlcLib.Base
{
    ///<summary>
    ///描述: PLC抽象实例类
    ///作者: 孙小同
    ///创建时间: 2024/6/10 22:17:23
    ///</summary>
    public abstract class PlcBase : ObservableObject, IPlc
    {

        //发布事件
        public abstract event Action<object, PlcEventArgs> OnMessage;
        public abstract event Action<object, PlcEventArgs> OnError;
        public abstract event Action<object, DataPackage> OnSend;
        public abstract event Action<object, DataPackage> OnReceived;

        protected Communication communication = new Communication();//与下位机的通讯类
        protected CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();//线程取消

        public abstract void Initialize(Net net);
        public abstract IPlcResult Start(Action action);

        public virtual void Stop()
        {
            communication.Stop();
            cancellationTokenSource.Cancel();
        }

        /// <summary>
        /// 通讯状态
        /// </summary>
        private ConnectedState connectedState;
        public ConnectedState ConnectedState
        {
            get { return connectedState; }
            set { connectedState = value; RaisePropertyChanged(); }
        }
    }
}
